
import { ipcMain, Menu, Tray } from 'electron'

import { appInfoConfig } from '@/config'

const path = require('path')

function SystemTray(thisApp,thisWin){
    let appTray = null;
    let appTrayTwinkleTask = null;
    const iconPathGrey = path.join(__static, 'app1.ico');
    const iconPath = path.join(__static, 'app.ico');
    let defaultIconPath = iconPathGrey;
    let hasNewMsg = false;
    function createTray(){
        appTray = new Tray(defaultIconPath);
        appTray.setToolTip(appInfoConfig.APP_NAME);
        createContextMenu();
        initEvent();
    }

    function createContextMenu(){
        let trayContextMenuTemplate = [{
            label: '退出',
            click: function () {
                appTray.destroy();
                appTray = null;
                thisApp.quit();
                thisApp.quit();
            }
        }];
        const contextMenu = Menu.buildFromTemplate(trayContextMenuTemplate);
        appTray.setContextMenu(contextMenu);
    }
    function initEvent(){
        appTray.on('click', function () {
            msgReaded();
            thisWin.show();
        })
        ipcMain.on('recive-new-msg', (event,msg) => {
            reciveNewMsg(msg);
        })
        ipcMain.on('chat-server-connected', (event,msg) => {
            chatServerConnected();
        })
        ipcMain.on('chat-server-disconnected', (event,msg) => {
            chatServerDisconnected();
        })
    }
    function startTrayTwinkle(){
        if(appTrayTwinkleTask){
            return;
        }
        let flag = true;
        appTrayTwinkleTask = setInterval(function () {
            if (flag) {
                appTray.setImage(iconPath);
            } else {
                appTray.setImage(iconPathGrey);
            }
            flag = !flag
        }, 200);
    }
    function stopTrayTwinkle(){
        if(appTrayTwinkleTask){
            clearInterval(appTrayTwinkleTask)
            appTrayTwinkleTask = null;
            appTray.setImage(defaultIconPath);
        }
    }
    function msgReaded(){
        hasNewMsg = false;
        stopTrayTwinkle();
    }

    function reciveNewMsg(msg){
        hasNewMsg = true;
        appTray.displayBalloon({
            iconType:'error',
            title:msg.name || '异常监控消息',
            content:msg.content || '未知异常，请注意',
            largeIcon:false
        });
        startTrayTwinkle()
    }

    function showInfoMsg(msg){
        appTray.displayBalloon({
            iconType:'info',
            title:'提醒消息',
            content:msg,
            largeIcon:false
        });
    }

    function chatServerConnected(){
        showInfoMsg("服务器连接成功！");
        defaultIconPath = iconPath;
        if(hasNewMsg){
            startTrayTwinkle();
        }else{
            appTray.setImage(defaultIconPath);
        }
    }
    function chatServerDisconnected(){
        defaultIconPath = iconPathGrey;
        stopTrayTwinkle();
        showInfoMsg("服务器已断开连接！");
    }
    createTray()
    return appTray;
}

export default SystemTray;